package org.loofer.ext.utils;

import android.database.Cursor;
import org.loofer.ext.Ext;
import org.loofer.ext.component.logger.Logger;
import org.loofer.ext.utils.CryptoUtils.MD5;
import com.tencent.connect.common.Constants;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;

public class IoUtils {
    private static final int BUFFER_SIZE = 1024;
    private static final int CONNECT_TIMEOUT = 3000;
    private static final String TAG = "IoUtils";

    public static void closeSilently(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (Throwable th) {
            }
        }
    }

    public static void closeSilently(Cursor cursor) {
        if (cursor != null && !cursor.isClosed()) {
            try {
                cursor.close();
            } catch (Throwable th) {
            }
        }
    }

    public static String inputStreamToString(InputStream is, String charset) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));
        boolean isFirstLine = true;
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                return sb.toString();
            }
            if (!isFirstLine) {
                sb.append("\n");
            }
            isFirstLine = false;
            sb.append(line);
        }
    }

    public static byte[] inputStreamToBytes(InputStream is) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        while (true) {
            int length = is.read(buffer, 0, 1024);
            if (length == -1) {
                return baos.toByteArray();
            }
            baos.write(buffer, 0, length);
            baos.flush();
        }
    }

    public static String readStringFromAssets(String fileName) {
        return readStringFromAssets(fileName, "UTF-8");
    }

    public static String readStringFromAssets(String fileName, String encoding) {
        String inputStreamToString;
        InputStream is = null;
        try {
            is = Ext.getContext().getAssets().open(fileName);
            inputStreamToString = inputStreamToString(is, encoding);
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    Logger.e(TAG, e.getMessage(), e);
                }
            }
        } catch (Exception e2) {
            Logger.e(TAG, e2.getMessage(), e2);
            inputStreamToString = "";
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e3) {
                    Logger.e(TAG, e3.getMessage(), e3);
                }
            }
        } catch (Throwable th) {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e32) {
                    Logger.e(TAG, e32.getMessage(), e32);
                }
            }
        }
        return inputStreamToString;
    }

    public static String readStringFromRawResource(int resId) {
        return readStringFromRawResource(resId, "UTF-8");
    }

    public static String readStringFromRawResource(int resId, String encoding) {
        String inputStreamToString;
        InputStream is = null;
        try {
            is = Ext.getContext().getResources().openRawResource(resId);
            inputStreamToString = inputStreamToString(is, encoding);
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    Logger.e(TAG, e.getMessage(), e);
                }
            }
        } catch (Exception e2) {
            Logger.e(TAG, e2.getMessage(), e2);
            inputStreamToString = "";
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e3) {
                    Logger.e(TAG, e3.getMessage(), e3);
                }
            }
        } catch (Throwable th) {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e32) {
                    Logger.e(TAG, e32.getMessage(), e32);
                }
            }
        }
        return inputStreamToString;
    }

    public static String readStringFromFile(String filePath) {
        return readStringFromFile(filePath, "UTF-8");
    }

    public static String readStringFromFile(String filePath, String encoding) {
        try {
            return new String(readBytesFromFile(filePath), encoding);
        } catch (Exception e) {
            Logger.e(TAG, e.getMessage(), e);
            return "";
        }
    }

    public static byte[] readBytesFromFile(String filePath) {
        return readBytesFromFile(new File(filePath));
    }

    public static byte[] readBytesFromFile(File file) {
        byte[] inputStreamToBytes;
        Exception e;
        Throwable th;
        FileInputStream fis = null;
        try {
            FileInputStream fis2 = new FileInputStream(file);
            try {
                inputStreamToBytes = inputStreamToBytes(fis2);
                if (fis2 != null) {
                    try {
                        fis2.close();
                    } catch (IOException e2) {
                        Logger.e(TAG, e2.getMessage(), e2);
                    }
                }
                fis = fis2;
            } catch (Exception e3) {
                e = e3;
                fis = fis2;
                try {
                    Logger.e(TAG, e.getMessage(), e);
                    inputStreamToBytes = new byte[0];
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e22) {
                            Logger.e(TAG, e22.getMessage(), e22);
                        }
                    }
                    return inputStreamToBytes;
                } catch (Throwable th2) {
                    th = th2;
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e222) {
                            Logger.e(TAG, e222.getMessage(), e222);
                        }
                    }
                    throw th;
                }
            } catch (Throwable th3) {
                th = th3;
                fis = fis2;
                if (fis != null) {
                    fis.close();
                }
                throw th;
            }
        } catch (Exception e4) {
            e = e4;
            Logger.e(TAG, e.getMessage(), e);
            inputStreamToBytes = new byte[0];
            if (fis != null) {
                fis.close();
            }
            return inputStreamToBytes;
        }
        return inputStreamToBytes;
    }

    public static byte[] readBytesFromRawResource(int resId) {
        byte[] inputStreamToBytes;
        InputStream is = null;
        try {
            is = Ext.getContext().getResources().openRawResource(resId);
            inputStreamToBytes = inputStreamToBytes(is);
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    Logger.e(TAG, e.getMessage(), e);
                }
            }
        } catch (Exception e2) {
            Logger.e(TAG, e2.getMessage(), e2);
            inputStreamToBytes = new byte[0];
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e3) {
                    Logger.e(TAG, e3.getMessage(), e3);
                }
            }
        } catch (Throwable th) {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e32) {
                    Logger.e(TAG, e32.getMessage(), e32);
                }
            }
        }
        return inputStreamToBytes;
    }

    public static byte[] readBytesFromURL(URL url) {
        byte[] inputStreamToBytes;
        InputStream is = null;
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod(Constants.HTTP_GET);
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3000);
            conn.connect();
            is = conn.getInputStream();
            inputStreamToBytes = inputStreamToBytes(is);
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (IOException e) {
                    Logger.e(TAG, e.getMessage(), e);
                }
            }
            if (is != null) {
                is.close();
            }
        } catch (Exception e2) {
            Logger.e(TAG, e2.getMessage(), e2);
            inputStreamToBytes = new byte[0];
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (IOException e3) {
                    Logger.e(TAG, e3.getMessage(), e3);
                }
            }
            if (is != null) {
                is.close();
            }
        } catch (Throwable th) {
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (IOException e32) {
                    Logger.e(TAG, e32.getMessage(), e32);
                }
            }
            if (is != null) {
                is.close();
            }
        }
        return inputStreamToBytes;
    }

    public static boolean writeToFile(File file, String text) {
        return writeToFile(file, text, "UTF-8");
    }

    public static boolean writeToFile(File file, String text, String encoding) {
        try {
            return writeToFile(file, text.getBytes(encoding));
        } catch (Exception e) {
            Logger.e(TAG, e.getMessage(), e);
            return false;
        }
    }

    public static boolean writeToFile(File file, byte[] buffer) {
        return writeToFile(file, new ByteArrayInputStream(buffer));
    }

    public static boolean writeToFile(File file, InputStream is) {
        Exception e;
        Throwable th;
        boolean z = false;
        FileOutputStream fileOutputStream = null;
        try {
            FileOutputStream fos = new FileOutputStream(file);
            try {
                byte[] buffer = new byte[1024];
                while (true) {
                    int length = is.read(buffer, 0, 1024);
                    if (length == -1) {
                        break;
                    }
                    fos.write(buffer, 0, length);
                    fos.flush();
                }
                z = true;
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e2) {
                        Logger.e(TAG, e2.getMessage(), e2);
                    }
                }
                fileOutputStream = fos;
            } catch (Exception e3) {
                e = e3;
                fileOutputStream = fos;
                try {
                    Logger.e(TAG, e.getMessage(), e);
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e22) {
                            Logger.e(TAG, e22.getMessage(), e22);
                        }
                    }
                    return z;
                } catch (Throwable th2) {
                    th = th2;
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e222) {
                            Logger.e(TAG, e222.getMessage(), e222);
                        }
                    }
                    throw th;
                }
            } catch (Throwable th3) {
                th = th3;
                fileOutputStream = fos;
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
                throw th;
            }
        } catch (Exception e4) {
            e = e4;
            Logger.e(TAG, e.getMessage(), e);
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
            return z;
        }
        return z;
    }

    public static File downloadToFile(String url, String path) {
        return downloadToFile(url, path, null);
    }

    public static File downloadToFile(String url, String path, String fileName) {
        File file;
        HttpURLConnection conn = null;
        InputStream is = null;
        try {
            conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setRequestMethod(Constants.HTTP_GET);
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3000);
            conn.connect();
            is = conn.getInputStream();
            if (fileName == null) {
                fileName = makeFileNameFromUrl(url);
            }
            file = new File(path, fileName);
            writeToFile(file, is);
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (IOException e) {
                    Logger.e(TAG, e.getMessage(), e);
                }
            }
            if (is != null) {
                is.close();
            }
        } catch (Exception e2) {
            Logger.e(TAG, e2.getMessage(), e2);
            file = null;
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (IOException e3) {
                    Logger.e(TAG, e3.getMessage(), e3);
                }
            }
            if (is != null) {
                is.close();
            }
        } catch (Throwable th) {
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (IOException e32) {
                    Logger.e(TAG, e32.getMessage(), e32);
                }
            }
            if (is != null) {
                is.close();
            }
        }
        return file;
    }

    public static File releaseAssertToFile(String assertName, String toPath) {
        InputStream is = null;
        File file = new File(toPath, assertName);
        try {
            makedirs(toPath);
            is = Ext.getContext().getAssets().open(assertName);
            if (writeToFile(file, is)) {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        Logger.e(TAG, e.getMessage(), e);
                    }
                }
                return null;
            } else if (is == null) {
                return file;
            } else {
                try {
                    is.close();
                    return file;
                } catch (IOException e2) {
                    Logger.e(TAG, e2.getMessage(), e2);
                    return file;
                }
            }
        } catch (Exception e3) {
            Logger.e(TAG, e3.getMessage(), e3);
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e22) {
                    Logger.e(TAG, e22.getMessage(), e22);
                }
            }
            return null;
        } catch (Throwable th) {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e222) {
                    Logger.e(TAG, e222.getMessage(), e222);
                }
            }
        }
    }

    public static File releaseRawToFile(int rawResId, String toPath, String toFileName) {
        InputStream is = null;
        File file = new File(toPath, toFileName);
        try {
            makedirs(toPath);
            is = Ext.getContext().getResources().openRawResource(rawResId);
            if (writeToFile(file, is)) {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        Logger.e(TAG, e.getMessage(), e);
                    }
                }
                return null;
            } else if (is == null) {
                return file;
            } else {
                try {
                    is.close();
                    return file;
                } catch (IOException e2) {
                    Logger.e(TAG, e2.getMessage(), e2);
                    return file;
                }
            }
        } catch (Exception e3) {
            Logger.e(TAG, e3.getMessage(), e3);
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e22) {
                    Logger.e(TAG, e22.getMessage(), e22);
                }
            }
            return null;
        } catch (Throwable th) {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e222) {
                    Logger.e(TAG, e222.getMessage(), e222);
                }
            }
        }
    }

    public static String makeFileNameFromUrl(String url) {
        if (url == null) {
            return null;
        }
        int start = url.indexOf("//");
        if (start == -1) {
            start = 0;
        }
        start = url.indexOf(47, start + 2);
        if (start == -1) {
            start = 0;
        }
        return url.substring(start).replaceAll("[^\\w\\-_]", "");
    }

    public static String makeMD5FileNameFromUrl(String url) {
        if (url == null) {
            return null;
        }
        return CryptoUtils.bytesToHexString(MD5.toMd5(url));
    }

    public static boolean makedirs(String dirPath) {
        File file = new File(dirPath);
        return file.mkdirs() || (file.exists() && file.isDirectory());
    }

    public static void copyFile(String oldPath, String newPath) {
        Exception e;
        Throwable th;
        FileInputStream fin = null;
        FileOutputStream fout = null;
        try {
            if (new File(oldPath).exists()) {
                FileInputStream fin2 = new FileInputStream(oldPath);
                try {
                    FileOutputStream fout2 = new FileOutputStream(newPath);
                    try {
                        byte[] buffer = new byte[1444];
                        while (true) {
                            int byteRead = fin2.read(buffer);
                            if (byteRead == -1) {
                                break;
                            }
                            fout2.write(buffer, 0, byteRead);
                        }
                        fout = fout2;
                        fin = fin2;
                    } catch (Exception e2) {
                        e = e2;
                        fout = fout2;
                        fin = fin2;
                        try {
                            e.printStackTrace();
                            if (fin != null) {
                                try {
                                    fin.close();
                                } catch (Exception e22) {
                                    e22.printStackTrace();
                                }
                            }
                            if (fout == null) {
                                try {
                                    fout.close();
                                } catch (Exception e222) {
                                    e222.printStackTrace();
                                    return;
                                }
                            }
                        } catch (Throwable th2) {
                            th = th2;
                            if (fin != null) {
                                try {
                                    fin.close();
                                } catch (Exception e2222) {
                                    e2222.printStackTrace();
                                }
                            }
                            if (fout != null) {
                                try {
                                    fout.close();
                                } catch (Exception e22222) {
                                    e22222.printStackTrace();
                                }
                            }
                            throw th;
                        }
                    } catch (Throwable th3) {
                        th = th3;
                        fout = fout2;
                        fin = fin2;
                        if (fin != null) {
                            fin.close();
                        }
                        if (fout != null) {
                            fout.close();
                        }
                        throw th;
                    }
                } catch (Exception e3) {
                    e = e3;
                    fin = fin2;
                    e.printStackTrace();
                    if (fin != null) {
                        fin.close();
                    }
                    if (fout == null) {
                        fout.close();
                    }
                } catch (Throwable th4) {
                    th = th4;
                    fin = fin2;
                    if (fin != null) {
                        fin.close();
                    }
                    if (fout != null) {
                        fout.close();
                    }
                    throw th;
                }
            }
            if (fin != null) {
                try {
                    fin.close();
                } catch (Exception e222222) {
                    e222222.printStackTrace();
                }
            }
            if (fout != null) {
                try {
                    fout.close();
                } catch (Exception e2222222) {
                    e2222222.printStackTrace();
                }
            }
        } catch (Exception e4) {
            e = e4;
            e.printStackTrace();
            if (fin != null) {
                fin.close();
            }
            if (fout == null) {
                fout.close();
            }
        }
    }

    public static String readfile(String filepath) {
        String filecontent = "";
        File file = new File(filepath);
        if (!file.isDirectory()) {
            return readStringFromFile(filepath);
        }
        if (file.isDirectory()) {
            String[] filelist = file.list();
            int i = 0;
            while (i < filelist.length) {
                File readfile = new File(filepath + File.separator + filelist[i]);
                if (readfile.isDirectory()) {
                    if (readfile.isDirectory()) {
                        readfile(filepath + File.pathSeparator + filelist[i]);
                    }
                    i++;
                } else {
                    filecontent = readStringFromFile(filepath + File.separator + filelist[i]);
                    filepath = filepath + File.separator + filelist[i];
                    return filecontent;
                }
            }
        }
        return "";
    }

    public static int getFileSize(String path) {
        File file = new File(path);
        String[] files = null;
        if (file.isDirectory()) {
            files = file.list();
        }
        if (files != null) {
            return files.length;
        }
        return 0;
    }

    public static boolean deleteFile(String sPath) {
        File file = new File(sPath);
        if (!file.isFile() || !file.exists()) {
            return false;
        }
        file.delete();
        return true;
    }

    public static void deleteOldFile(String path, int day) {
        Calendar cdweek = Calendar.getInstance();
        cdweek.add(5, -day);
        Date d = cdweek.getTime();
        File fileBag = new File(path);
        if (fileBag.isDirectory()) {
            String[] filesName = fileBag.list();
            for (String str : filesName) {
                File file = new File(path + str);
                Long time = Long.valueOf(file.lastModified());
                Calendar cd = Calendar.getInstance();
                cd.setTimeInMillis(time.longValue());
                if (cd.getTime().before(d)) {
                    file.delete();
                }
            }
        }
    }
}
